package com.example.micrauth.domain.vo;

import cn.hutool.core.util.ObjectUtil;
import com.example.micrapi.pojo.SysUser;
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonSetter;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description: 用户信息
 * @author: uu
 * @date: 2024/2/28 19:08
 * @version: 1.0
 */
@Data
@Slf4j
@JsonIgnoreProperties(ignoreUnknown = true)
public class LoginUserVO implements UserDetails, Serializable {

    private Long id;
    private String token;
    // 用户信息
    private SysUser sysUser = new SysUser();

    @JsonIgnore
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<String> codeList = sysUser.getCodeList();
        if (ObjectUtil.isNotEmpty(codeList)) {
            List<SimpleGrantedAuthority> collect = codeList.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
            log.info("LoginUserVO getAuthorities collect = {}", collect);
            return collect;
        }
        return null;
    }
    @JsonIgnore
    @Override
    public String getPassword() {
        return sysUser.getPassword();
    }
    @JsonIgnore
    @Override
    public String getUsername() {
        return sysUser.getUsername();
    }
    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return sysUser.getCredentialsNoExpired() == 0;
    }
    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return sysUser.getAccountNoLocked() == 0;
    }
    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return sysUser.getCredentialsNoExpired() == 0;
    }
    @JsonIgnore
    @Override
    public boolean isEnabled() {
        return sysUser.getEnabled() == 0;
    }
}
